import {reactive} from "vue";
import {CopyProperty} from "hongshu-view";



function Page(loadFunction){
    const pageInfo = reactive({
        pageIndex:1,
        pageSize:10,
        total:0
    });

    const ResetPage = (reset)=>{

        if(reset && reset.hasOwnProperty('pageIndex')){
            CopyProperty(pageInfo,reset);
        }else{
            pageInfo.pageIndex =1;
            pageInfo.total = 0;
        }
        loadFunction && loadFunction();
        
    }

    const PageIndexChange = (param)=>{
        loadFunction && loadFunction();
    }

    const PageSizeChange = ()=>{
        loadFunction && loadFunction();
    }

    const ReduceCount =(count =1)=>{
        let originTotalPage = Math.ceil(pageInfo.total/pageInfo.pageSize);
        if(pageInfo.pageIndex < originTotalPage){
            loadFunction && loadFunction();
            return;
        }

        let remindTotalPage = Math.ceil((pageInfo.total - count)/pageInfo.pageSize);
        if(remindTotalPage == 0){

        }else if(remindTotalPage < originTotalPage){
            pageInfo.pageIndex = remindTotalPage;
        }
        loadFunction && loadFunction();
    }

    return {
        pageInfo,
        pageSizes:[10, 20, 30, 40, 50],
        ResetPage,
        PageIndexChange,
        PageSizeChange,
        ReduceCount,
    }
};

export {
    Page,
}

